package com.SouthernWall_404.AgeRight.Common.API.Capability;

import com.SouthernWall_404.AgeRight.Common.API.MissionAPI.MainMissionInfo;
import com.SouthernWall_404.AgeRight.Common.Network.ModChannels;
import com.SouthernWall_404.AgeRight.Common.Network.Packets.S2C.MissionsSyncPacket;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.common.util.INBTSerializable;

import java.util.ArrayList;
import java.util.List;

public class Missions implements INBTSerializable<CompoundTag> {

    private List<MainMissionInfo> activeMissions = new ArrayList<>();
    private List<MainMissionInfo> completedMissions = new ArrayList<>();

    /**
     * 同步数据到客户端
     */
    public void syncToClient(Player player) {
        if (!player.level().isClientSide() && player instanceof ServerPlayer serverPlayer) {
            ModChannels.sendToClient(
                    new MissionsSyncPacket(this.serializeNBT()),
                    serverPlayer
            );
            System.out.println("向客户端同步任务数据，活跃任务数量: " + activeMissions.size());
        }
    }

    /**
     * 添加一个新任务
     */
    public void addMission(MainMissionInfo mission, Player player) {
        if (mission != null && !activeMissions.contains(mission)) {
            activeMissions.add(mission);
            syncToClient(player); // 同步到客户端
        }
    }

    /**
     * 完成任务
     */
    public void completeMission(MainMissionInfo mission, Player player) {
        if (activeMissions.contains(mission)) {
            activeMissions.remove(mission);
            completedMissions.add(mission);
            syncToClient(player); // 同步到客户端
        }
    }

    /**
     * 获取所有活跃任务
     */
    public List<MainMissionInfo> getActiveMissions() {
        return new ArrayList<>(activeMissions);
    }

    /**
     * 获取所有已完成任务
     */
    public List<MainMissionInfo> getCompletedMissions() {
        return new ArrayList<>(completedMissions);
    }

    /**
     * 检查击杀事件
     */
    public void checkKill(LivingEntity livingEntity, Player player) {
        boolean updated = false;
        for (MainMissionInfo mission : activeMissions) {
            mission.checkKill(livingEntity, player);
            if (mission.isFininshed) {
                completeMission(mission, player);
                updated = true;
            } else {
                // 检查任务进度是否有更新
                updated = true;
            }
        }
        if (updated) {
            syncToClient(player);
        }
    }

    /**
     * 检查收集事件
     */
    public void checkCollect(ItemStack itemStack, Player player) {
        boolean updated = false;
        for (MainMissionInfo mission : activeMissions) {
            mission.checkCollect(itemStack, player);
            if (mission.isFininshed) {
                completeMission(mission, player);
                updated = true;
            } else {
                updated = true;
            }
        }
        if (updated) {
            syncToClient(player);
        }
    }

    /**
     * 检查位置事件
     */
    public void checkPosition(BlockPos blockPos, Player player) {
        boolean updated = false;
        for (MainMissionInfo mission : activeMissions) {
            mission.checkPosition(blockPos, player);
            if (mission.isFininshed) {
                completeMission(mission, player);
                updated = true;
            } else {
                updated = true;
            }
        }
        if (updated) {
            syncToClient(player);
        }
    }

    /**
     * 检查所有任务状态
     */
    public void checkAllMissions(Player player) {
        boolean updated = false;
        for (MainMissionInfo mission : activeMissions) {
            mission.check(player);
            if (mission.isFininshed) {
                completeMission(mission, player);
                updated = true;
            } else {
                updated = true;
            }
        }
        if (updated) {
            syncToClient(player);
        }
    }

    @Override
    public CompoundTag serializeNBT() {
        CompoundTag nbt = new CompoundTag();

        // 保存活跃任务
        ListTag activeMissionsList = new ListTag();
        for (MainMissionInfo mission : activeMissions) {
            activeMissionsList.add(mission.getModPack());
        }
        nbt.put("active_missions", activeMissionsList);

        // 保存已完成任务
        ListTag completedMissionsList = new ListTag();
        for (MainMissionInfo mission : completedMissions) {
            completedMissionsList.add(mission.getModPack());
        }
        nbt.put("completed_missions", completedMissionsList);

        return nbt;
    }

    @Override
    public void deserializeNBT(CompoundTag nbt) {
        activeMissions.clear();
        completedMissions.clear();

        // 加载活跃任务
        if (nbt.contains("active_missions")) {
            ListTag activeMissionsList = nbt.getList("active_missions", 10);
            for (int i = 0; i < activeMissionsList.size(); i++) {
                CompoundTag missionTag = activeMissionsList.getCompound(i);
                MainMissionInfo mission = new MainMissionInfo();
                mission.loadFromModPack(missionTag);
                activeMissions.add(mission);
            }
        }

        // 加载已完成任务
        if (nbt.contains("completed_missions")) {
            ListTag completedMissionsList = nbt.getList("completed_missions", 10);
            for (int i = 0; i < completedMissionsList.size(); i++) {
                CompoundTag missionTag = completedMissionsList.getCompound(i);
                MainMissionInfo mission = new MainMissionInfo();
                mission.loadFromModPack(missionTag);
                completedMissions.add(mission);
            }
        }

        System.out.println("反序列化任务数据: " + activeMissions.size() + " 个活跃任务");
    }
}